<template>
  <el-dialog :visible.sync="visible" title="编目新建" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
      <!-- <el-form-item label="编号" prop="code">
        <el-input v-model="dataForm.code" placeholder=""></el-input>
      </el-form-item> -->
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称" :disabled="isExamine"></el-input>
      </el-form-item>
       <el-form-item prop="pid" label="目录级别" class="menu-list">
        <el-popover  v-model="menuListVisible" ref="menuListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="catalogueTree"
            :props="{ label: 'name', children: 'children' }"
            node-key="id"
            :disabled="isExamine"
            ref="menuListTree"
            :highlight-current="true"
            :expand-on-click-node="false"
            accordion
            @current-change="menuListTreeCurrentChangeHandle">
          </el-tree>
        </el-popover>
        <el-input :disabled="isExamine" v-model="dataForm.parentName"  class="icon-input" v-popover:menuListPopover :readonly="true" placeholder="目录级别">
          <i v-if="dataForm.pid !== '0'" slot="suffix" @click.stop="deptListTreeSetDefaultHandle()" class="el-icon-circle-close el-input__icon"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序" :disabled="isExamine"></el-input>
      </el-form-item>
      <el-form-item prop="icon" :label="$t('menu.icon')" class="icon-list">
        <el-popover v-model="iconListVisible" ref="iconListPopover" placement="bottom-start" trigger="click" popper-class="mod-sys__menu-icon-popover">
          <div class="mod-sys__menu-icon-inner">
            <div class="mod-sys__menu-icon-list">
              <el-button
                v-for="(item, index) in iconList"
                :key="index"
                 :disabled="isExamine"
                @click="iconListCurrentChangeHandle(item)"
                :class="{ 'is-active': dataForm.icon === item }">
                <svg class="icon-svg" aria-hidden="true"><use :xlink:href="`#${item}`"></use></svg>
              </el-button>
            </div>
          </div>
        </el-popover>
        <el-input :disabled="isExamine"  v-model="dataForm.icon" v-popover:iconListPopover :readonly="true" class="icon-input" :placeholder="$t('menu.icon')"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-select v-model="dataForm.department" placeholder="请选择" :disabled="isExamine">
          <el-option v-for="item in departmentSelects" :key="item.label" v-bind="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="简介" prop="remark2">
        <el-input type="textarea" v-model="dataForm.remark2" :disabled="isExamine"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer" >
      <el-button v-if="!isExamine" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button v-if="!isExamine" type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
      <el-button v-if="isExamine" @click="dataFormSubmitHandle(2)">驳回</el-button>
      <el-button v-if="isExamine" type="primary" @click="dataFormSubmitHandle(1)">同意</el-button>
    </template>
  </el-dialog>
</template>

<script>

import debounce from 'lodash/debounce'
import { getIconList } from '@/utils'
import { mapState } from 'vuex'
import dictionaries from '@/utils/dictionaries.js'

export default {
  data () {
    return {
      companyArr: dictionaries.companyArr,
      menuListVisible: false,
      iconListVisible: false,
      visible: false,
      iconList: [],
      dataForm: {
        id: '',
        classId: '',
        code: '',
        name: '',
        pid: '',
        level: '',
        sort: '',
        isDelete: '',
        icon: '',
        department: '',
        remark2: '',
        remark3: '',
        remark4: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        state: '',
        parentName: ''
      }
    }
  },
  props: {
    isExamine: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapState(['catalogueTree']),
    ...mapState(['departmentSelects']),
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        // pid: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'change' }
        // ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        icon: [
          { type: 'string', required: true, message: this.$t('validate.required'), trigger: 'change' }
        ],
        department: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 上级菜单树, 设置默认值
    deptListTreeSetDefaultHandle () {
      this.dataForm.pid = '0'
      this.dataForm.parentName = '目录级别'
    },
    // 图标, 选中
    iconListCurrentChangeHandle (icon) {
      this.dataForm.icon = icon
      this.iconListVisible = false
    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        this.iconList = getIconList()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 上级菜单树, 选中
    menuListTreeCurrentChangeHandle (data) {
      this.dataForm.pid = data.id
      this.dataForm.parentName = data.name
      this.menuListVisible = false
    },
    // 获取信息
    getInfo () {
      this.$http.get('/bscatalogue/bscatalogue/' + this.dataForm.id).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        if (this.dataForm.pid === '0') {
          return this.deptListTreeSetDefaultHandle()
        }
        this.$refs.menuListTree.setCurrentKey(this.dataForm.pid)
      }).catch(() => {})
    },
    cancelRequest () {
      this.$prompt('请输入驳回理由', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.dataForm.opinion = value
        this.dataForm.state = 2
        this.$http[!this.dataForm.id ? 'post' : 'put']('/bscatalogue/bscatalogue/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      }).catch(() => {

      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function (isType = 3) {
      if (isType == 2) {
        this.cancelRequest()
        return
      } else if (isType == 1) {
        this.dataForm.state = 1
      }
      this.$refs.dataForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/bscatalogue/bscatalogue/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { leading: true, trailing: false })
  }
}
</script>

<style lang="scss" scoped>
.icon-input ::v-deep  .el-input__inner {
  cursor: pointer;
}
</style>

<style lang="scss">
.mod-sys__menu {
  .menu-list,
  .icon-list {
    .el-input__inner,
    .el-input__suffix {
      cursor: pointer;
    }
  }
  &-icon-popover {
    width: 458px;
    overflow: hidden;
  }
  &-icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  &-icon-list {
    width: 458px;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
}
</style>
